import psycController from './controller/psycController';
import * as koaCsrf from 'koa-csrf';
import * as Koa from 'koa';
import * as log from 'npmlog';
import * as bodyParser from 'koa-bodyparser';
import * as koaCros from 'koa2-cors';
import * as serve from 'koa-static';
import * as mount from 'koa-mount';
require('babel-polyfill');

var staticApp = new Koa();
staticApp.use(serve('public/dist'));

const port = process.env.PORT || 80;
const app = new Koa();
app.use(koaCros({
    origin: '*'
}));
app.use(bodyParser({ formLimit: '10mb' }));
app.use(psycController.routes()).use(psycController.allowedMethods());
app.use(new koaCsrf({
    invalidSessionSecretMessage: 'Invalid session secret',
    invalidSessionSecretStatusCode: 403,
    invalidTokenMessage: 'Invalid CSRF token',
    invalidTokenStatusCode: 403,
    excludedMethods: ['GET', 'HEAD', 'OPTIONS'],
    disableQuery: false
}));

const server = app.listen(port, (error?) => {
    if (error) {
        log.error({ type: 'startError', message: error.stack });
    } else {
        log.info(`==> Listening on port ${port}. `);
    }
});
